/*
*** 作用：将两个数组合并成一个数组
*** 例如：["a","b","c","d"]和[1,2,3,4]，转换成
	[
		{name:"a"，key:1},
		{name:"b"，key:2},
		{name:"c"，key:3},
		{name:"d"，key:4},
	]
*/

/*
 *** 申明变量
 */
var arr1 = ["a","b","c","d","e"];
var arr2 = [1,2,3,4,5,6,7];
var obj1 = { arr: arr1, key: "value" };
var obj2 = { arr: arr2, key: "name" };

/*
 *** 主函数，不允许修改。
 */
var fs = require("fs");

function array_to_object(obj1, obj2) {
    var arr1 = obj1.arr;
    var arr2 = obj2.arr;
    var key1 = obj1.key;
    var key2 = obj2.key;
    var result = [];
    if (arr1.length >= arr2.length) {
        for (var i = 0; i < arr1.length; i++) {
            var obj = {};
            obj[key1] = arr1[i];
            if(arr2[i]){
            	obj[key2] = arr2[i];
            }else{
            	obj[key2] = "";
            };
            result.push(obj);
        };
    }else{
    	for (var i = 0; i < arr2.length; i++) {
            var obj = {};
            obj[key2] = arr1[2];
            if(arr1[i]){
            	obj[key1] = arr1[i];
            }else{
            	obj[key1] = "";
            };
            result.push(obj);
        };
    }
    console.log("Result:",result);
    var result = JSON.stringify(result);
    fs.writeFileSync("./result/result.json", result);
};
array_to_object(obj1, obj2);
